package com.pen.interfece.controller.user;

import com.pen.interfece.pojo.PageResult;
import com.pen.interfece.pojo.Result;
import com.pen.interfece.pojo.dto.AdminLoginDTO;
import com.pen.interfece.pojo.dto.DisciplinaryPageQueryDTO;
import com.pen.interfece.pojo.dto.StudentPageQueryDTO;
import com.pen.interfece.pojo.dto.illPageQueryDTO;
import com.pen.interfece.pojo.entity.*;
import com.pen.interfece.pojo.vo.AdminLoginVO;
import com.pen.interfece.service.AdminService;
import com.pen.interfece.service.DisciplinarySerivice;
import com.pen.interfece.service.MaintenanceService;
import com.pen.interfece.service.UserService;
import com.pen.interfece.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    //微信端登录
    @Autowired
    private UserService userService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MaintenanceService maintenanceService;

    @Autowired
    private DisciplinarySerivice disciplinarySerivice;
    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO){
        log.info("用户登录");

        String account = adminLoginDTO.getAccount();
        String password = adminLoginDTO.getPassword();
        //调用service方法查询数据库
        Admin admin=adminService.login(adminLoginDTO);
        if(admin==null){
            return Result.error("账号不存在");
        }
        if(!admin.getPassword().equals(password)){
            return Result.error("密码错误");
        }
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("userId", admin.getId());
        String token = JwtUtil.genToken(claims);

        AdminLoginVO adminLoginVO =new AdminLoginVO();
        BeanUtils.copyProperties(admin, adminLoginVO);
        adminLoginVO.setToken(token);
        System.out.println(adminLoginVO);
        return Result.success(adminLoginVO);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Admin admin){
        log.info("用户注册:{}",admin);
        userService.register(admin);
        return Result.success("用户注册成功");

    }


    @PostMapping("/page")
    public Result<PageResult> page(@RequestBody illPageQueryDTO ill){
        log.info("该用户的报告查询：参数为：{}",ill);
        PageResult pageResult=userService.page(ill);
        return Result.success(pageResult);
    }
    @PostMapping("/save")
    public Result save(@RequestBody ill i) {
        log.info("保存病历:{}", i);
        userService.save(i);
        return Result.success("增加报修成功");

    }
    @PostMapping("/update")
    public Result update(@RequestBody Admin admin){
        log.info("编辑用户信息：参数为：{}",admin);
        userService.update(admin);
        return Result.success("修改成功");
    }


}
